var a,b;
require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var vm = new Vue({
    el:"#collageAdd",
    data:{
        isShare:false,
        loginUid:"",
        userInfo:{},
        isScreen:"",
        user:{},
        orderNumber:"",
        burstList:{},
        beginData:"",
        endData:"",
        integral:"",
        hours:"",//时
        minutes:"",//分
        seconds:"",//秒
        num:5
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
        this.getCouponByCouponId();

        // if(this.searchObject&&this.searchObject.loginUid){
        //     this.loginUid=this.searchObject&&this.searchObject.loginUid;
        // }else{
        //
        // }

    },
    methods:{
        //后退
        getBack: function () {
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //乐购-订单详情
        getCouponByCouponId:function(){
            var that=this,data;
            data={
                orderNumber:that.orderNumber
            };
            this.ajax({url:"/purchase/getCollageByOrderNumber",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.burstList=b.json.data;
                    that.compareDate(that.burstList.currentTime,that.burstList.endDate);
                    that.user=that.burstList.list[0];
                    that.beginData=that.burstList.beginTime.split(" ")[0];
                    that.endData=that.burstList.endTime.split(" ")[0];
                    if(that.burstList.lastNum>0){
                        if(!userInfo){
                            that.getIsScreen(1);
                            b=setInterval(that.hideTip, 1000);
                            //location.href="/loginIndex"
                        }else{
                            that.userInfo=userInfo;
                            that.loginUid=userInfo.id;
                        }
                    }
                }else{
                    vm.$refs.prompt.toast("获取详情失败！")
                }
            });
        },
        //获取用户乐币
        getUserIntegral:function(){
            var that=this,data;
            data={
                loginUid:that.loginUid
            };
            this.ajax({url:"/user/getUserIntegral",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.integral=b.json.data.integral;
                }else{
                    vm.$refs.prompt.toast("获取用户乐币失败！")
                }
            });
        },
        compareDate:function(startDate,endDate){
            var that=this;
            startDate = new Date(startDate.replace(/-/g, "/"));
            endDate = new Date(endDate.replace(/-/g, "/"));
            console.log(endDate)
            var ms = endDate.getTime() - startDate.getTime();
            if (ms < 0){
                return 0;
            }
            //计算出小时数
            var hours=Math.floor(ms/(3600*1000));
            //计算相差分钟数
            var leave2=ms%(3600*1000);        //计算小时数后剩余的毫秒数
            var minutes=Math.floor(leave2/(60*1000));
            //计算相差秒数
            var leave3=leave2%(60*1000);      //计算分钟数后剩余的毫秒数
            var seconds=Math.round(leave3/1000);
            that.hours=hours;
            that.minutes=minutes;
            that.seconds=seconds;
            if(that.hours>=0&&that.minutes>=0&&that.seconds>0){
                a=setInterval(this.leftTimer,1000);
            }
        },
        //时分秒倒计时
        leftTimer:function(){
            var that=this;
            that.hours=Number(that.hours);
            that.minutes=Number(that.minutes);
            that.seconds=Number(that.seconds);
            if(that.hours==0&&that.minutes==0&&that.seconds==0){
                clearInterval(a);
            }else{
                that.seconds--;
                if(that.seconds == 0){
                    that.minutes--;
                    that.seconds = 59;
                }
                if(that.minutes < 0 && that.hours > 0){
                    that.hours--;
                    that.minutes = 59;
                }
                if (that.hours <= 9) that.hours = '0' + that.hours;
                if (that.minutes <= 9) that.minutes = '0' + that.minutes;
                if (that.seconds <= 9) that.seconds = '0' + that.seconds;
            }
        },
        getIsScreen:function(num) {
            this.isScreen=num;
            if(this.isScreen==2){
                this.getUserIntegral();
            }
        },
        //拼团
        buyCoupon:function(){
            var that=this,data,url;
            if(that.burstList.lastNum==0){
                vm.$refs.prompt.toast("已拼团成功！");
                vm.$refs.prompt.toastInWechat("已拼团成功！");
                return false;
            }
            data={
                loginUid:that.loginUid,
                orderNumber:that.orderNumber
            };
            this.ajax({url:"/purchase/buyCollageByOrderId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.getNext(2);

                    // location.href="/purchase/collageOK";
                }else{
                    if(b.json.msg=="此券已卖光"){
                        that.getIsScreen();
                    }
                    vm.$refs.prompt.toast(b.json.msg||"拼单失败！");

                }
            });
        },
        hideTip:function(){
            this.num--;
            if(this.num==0){
                //clearInterval(b);
                this.getNext(1)
            }
        },
        //跳转页面
        getNext:function(type){
            if(type==1){
                location.href="/loginIndex?number=14&orderNumber="+this.orderNumber;
            }else if(type==2){
                location.href="/purchase/collageOK?orderNumber="+this.orderNumber;
            }else if(type==3){
                this.getIsScreen();
                vm.$refs.prompt.toastInWechat("请前往app充值！");
                vm.$refs.prompt.toast("请前往app充值！");
            }else if(type==4){
                location.href="/happyi/collage";
            }
        }
    }
});
function checkTime(i){ //将0-9的数字前面加上0，例1变为01
    if(i<10){
        i = "0" + i;
    }
    return i;
}